{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T04:59:35.694812Z",
     "iopub.status.busy": "2024-02-01T04:59:35.694812Z",
     "iopub.status.idle": "2024-02-01T04:59:36.217634Z",
     "shell.execute_reply": "2024-02-01T04:59:36.217634Z",
     "shell.execute_reply.started": "2024-02-01T04:59:35.694812Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\n",
      "version 18.0\n"
     ]
    }
   ],
   "source": [
    "clear all\n",
    "capture log close\n",
    "\n",
    "version"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Data import"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T04:59:36.222642Z",
     "iopub.status.busy": "2024-02-01T04:59:36.222642Z",
     "iopub.status.idle": "2024-02-01T04:59:37.001473Z",
     "shell.execute_reply": "2024-02-01T04:59:37.001473Z",
     "shell.execute_reply.started": "2024-02-01T04:59:36.222642Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(28 vars, 2,784 obs)\n"
     ]
    }
   ],
   "source": [
    "insheet using \"gravity.csv\", comma clear"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T04:59:37.005986Z",
     "iopub.status.busy": "2024-02-01T04:59:37.005986Z",
     "iopub.status.idle": "2024-02-01T04:59:37.660393Z",
     "shell.execute_reply": "2024-02-01T04:59:37.660393Z",
     "shell.execute_reply.started": "2024-02-01T04:59:37.005986Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "src_iso                distance               src_human_rights\n",
      "dest_iso               migrant_stock          src_demogr_pressures\n",
      "src_country            src_security_app       src_refugees_and_idps\n",
      "dest_country           src_fact_elites        src_ext_intervention\n",
      "month                  src_group_grievance    route_arrivals\n",
      "n_arrivals             src_economy            route_interceptions\n",
      "year                   src_econ_inequality    route_deaths\n",
      "region                 src_human_flight       pr_rescue\n",
      "src_gdp_per_capita     src_state_legitimacy\n",
      "dest_gdp_per_capita    src_public_services\n"
     ]
    }
   ],
   "source": [
    "ds, varwidth(32)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Generate variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T04:59:37.660393Z",
     "iopub.status.busy": "2024-02-01T04:59:37.660393Z",
     "iopub.status.idle": "2024-02-01T04:59:38.450147Z",
     "shell.execute_reply": "2024-02-01T04:59:38.447940Z",
     "shell.execute_reply.started": "2024-02-01T04:59:37.660393Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "encode src_iso, gen(src_iso_enc)\n",
    "encode dest_iso, gen(dest_iso_enc)\n",
    "encode month, gen(month_enc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T04:59:38.451547Z",
     "iopub.status.busy": "2024-02-01T04:59:38.451547Z",
     "iopub.status.idle": "2024-02-01T04:59:39.289196Z",
     "shell.execute_reply": "2024-02-01T04:59:39.287676Z",
     "shell.execute_reply.started": "2024-02-01T04:59:38.451547Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "gen src_dest = src_iso + \"_\" + dest_iso\n",
    "encode src_dest, gen(src_dest_enc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T04:59:39.289196Z",
     "iopub.status.busy": "2024-02-01T04:59:39.289196Z",
     "iopub.status.idle": "2024-02-01T04:59:40.028450Z",
     "shell.execute_reply": "2024-02-01T04:59:40.028450Z",
     "shell.execute_reply.started": "2024-02-01T04:59:39.289196Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "gen log_distance = log(distance)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T04:59:40.028450Z",
     "iopub.status.busy": "2024-02-01T04:59:40.028450Z",
     "iopub.status.idle": "2024-02-01T04:59:40.879226Z",
     "shell.execute_reply": "2024-02-01T04:59:40.876732Z",
     "shell.execute_reply.started": "2024-02-01T04:59:40.028450Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "gen inferred_n = n_arrivals / pr_rescue\n",
    "gen log_inferred_n = log(1+inferred_n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T04:59:40.881298Z",
     "iopub.status.busy": "2024-02-01T04:59:40.881298Z",
     "iopub.status.idle": "2024-02-01T04:59:41.750470Z",
     "shell.execute_reply": "2024-02-01T04:59:41.747177Z",
     "shell.execute_reply.started": "2024-02-01T04:59:40.881298Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "lab var src_security_app \"FSI: Security Apparatus\"\n",
    "lab var src_fact_elites \"FSI: Factionalized Elites\"         \n",
    "lab var src_group_grievance \"FSI: Group Grievance\"                  \n",
    "lab var src_economy \"FSI: Economic Decline\"            \n",
    "lab var src_econ_inequality \"FSI: Uneven Economic Development\"               \n",
    "lab var src_human_flight \"FSI: Human Flight and Brain Drain\"              \n",
    "lab var src_state_legitimacy \"FSI: State Legitimacy\"         \n",
    "lab var src_public_services \"FSI: Public Services\"\n",
    "lab var src_human_rights  \"FSI: Human Rights and Rule of Law\"      \n",
    "lab var src_demogr_pressures   \"FSI: Demographic Pressures\"   \n",
    "lab var src_refugees_and_idps     \"FSI: Refugees and IDPs\"\n",
    "lab var src_ext_intervention \"FSI: External Intervention\"\n",
    "lab var migrant_stock \"Migrant stock\"\n",
    "lab var pr_rescue \"Probability of Rescue\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Summarize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T04:59:41.752902Z",
     "iopub.status.busy": "2024-02-01T04:59:41.752902Z",
     "iopub.status.idle": "2024-02-01T04:59:42.568694Z",
     "shell.execute_reply": "2024-02-01T04:59:42.568694Z",
     "shell.execute_reply.started": "2024-02-01T04:59:41.752902Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of unique values of month is  48\n",
      "Number of records is  2784\n"
     ]
    }
   ],
   "source": [
    "unique month // Monthly observations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T04:59:42.568694Z",
     "iopub.status.busy": "2024-02-01T04:59:42.568694Z",
     "iopub.status.idle": "2024-02-01T04:59:43.372836Z",
     "shell.execute_reply": "2024-02-01T04:59:43.372836Z",
     "shell.execute_reply.started": "2024-02-01T04:59:42.568694Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of unique values of src_iso is  37\n",
      "Number of records is  2784\n"
     ]
    }
   ],
   "source": [
    "unique src_iso // Origin countries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T04:59:43.372836Z",
     "iopub.status.busy": "2024-02-01T04:59:43.372836Z",
     "iopub.status.idle": "2024-02-01T04:59:44.205989Z",
     "shell.execute_reply": "2024-02-01T04:59:44.202385Z",
     "shell.execute_reply.started": "2024-02-01T04:59:43.372836Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of unique values of src_iso dest_iso is  58\n",
      "Number of records is  2784\n"
     ]
    }
   ],
   "source": [
    "unique src_iso dest_iso // Origin-destination pairs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T04:59:44.205989Z",
     "iopub.status.busy": "2024-02-01T04:59:44.205989Z",
     "iopub.status.idle": "2024-02-01T04:59:45.008038Z",
     "shell.execute_reply": "2024-02-01T04:59:45.008038Z",
     "shell.execute_reply.started": "2024-02-01T04:59:44.205989Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "           |             dest_iso\n",
      "   src_iso |       ESP        GRC        ITA |     Total\n",
      "-----------+---------------------------------+----------\n",
      "       AFG |         0         48          0 |        48 \n",
      "       BFA |        48          0          0 |        48 \n",
      "       BGD |        48          0         48 |        96 \n",
      "       CAF |        48          0          0 |        48 \n",
      "       CIV |        48          0         48 |        96 \n",
      "       CMR |        48          0         48 |        96 \n",
      "       COD |        48         48          0 |        96 \n",
      "       COG |        48          0          0 |        48 \n",
      "       COM |        48          0          0 |        48 \n",
      "       DZA |        48         48         48 |       144 \n",
      "       EGY |        48          0         48 |        96 \n",
      "       ERI |         0          0         48 |        48 \n",
      "       ETH |         0          0         48 |        48 \n",
      "       GHA |        48          0         48 |        96 \n",
      "       GIN |        48          0         48 |        96 \n",
      "       GMB |        48          0         48 |        96 \n",
      "       GNB |        48          0          0 |        48 \n",
      "       IRN |         0         48          0 |        48 \n",
      "       IRQ |         0         48         48 |        96 \n",
      "       KWT |         0         48          0 |        48 \n",
      "       LBR |        48          0          0 |        48 \n",
      "       LBY |         0          0         48 |        48 \n",
      "       MAR |        48          0         48 |        96 \n",
      "       MLI |        48          0         48 |        96 \n",
      "       MRT |        48          0          0 |        48 \n",
      "       NGA |        48          0         48 |        96 \n",
      "       PAK |         0         48         48 |        96 \n",
      "       PSE |        48         48          0 |        96 \n",
      "       SDN |         0          0         48 |        48 \n",
      "       SEN |        48          0         48 |        96 \n",
      "       SLE |        48          0         48 |        96 \n",
      "       SOM |         0          0         48 |        48 \n",
      "       SYR |        48         48         48 |       144 \n",
      "       TCD |        48          0          0 |        48 \n",
      "       TGO |        48          0          0 |        48 \n",
      "       TUN |        48          0         48 |        96 \n",
      "       YEM |        48          0          0 |        48 \n",
      "-----------+---------------------------------+----------\n",
      "     Total |     1,296        432      1,056 |     2,784 \n"
     ]
    }
   ],
   "source": [
    "tab src_iso dest_iso "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T04:59:45.015383Z",
     "iopub.status.busy": "2024-02-01T04:59:45.008038Z",
     "iopub.status.idle": "2024-02-01T04:59:45.818701Z",
     "shell.execute_reply": "2024-02-01T04:59:45.818701Z",
     "shell.execute_reply.started": "2024-02-01T04:59:45.008038Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of unique values of src_dest is  32\n",
      "Number of records is  1536\n"
     ]
    }
   ],
   "source": [
    "unique src_dest if migrant_stock!=. // Country pairs with complete records"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Set up time series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T04:59:45.818701Z",
     "iopub.status.busy": "2024-02-01T04:59:45.818701Z",
     "iopub.status.idle": "2024-02-01T04:59:46.635967Z",
     "shell.execute_reply": "2024-02-01T04:59:46.635010Z",
     "shell.execute_reply.started": "2024-02-01T04:59:45.818701Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\n",
      "\n",
      "Panel variable: src_dest_enc (strongly balanced)\n",
      " Time variable: date, 01jan2016 to 01dec2019, but with gaps\n",
      "         Delta: 1 day\n",
      "\n"
     ]
    }
   ],
   "source": [
    "gen date = date(month, \"YMD\")\n",
    "format date %td\n",
    "\n",
    "tsset src_dest_enc date\n",
    "sort src_dest_enc date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T04:59:46.636964Z",
     "iopub.status.busy": "2024-02-01T04:59:46.636964Z",
     "iopub.status.idle": "2024-02-01T04:59:47.451516Z",
     "shell.execute_reply": "2024-02-01T04:59:47.450564Z",
     "shell.execute_reply.started": "2024-02-01T04:59:46.636964Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "(58 missing values generated)\n",
      "\n",
      "(1,632 missing values generated)\n",
      "\n",
      "(1,632 missing values generated)\n"
     ]
    }
   ],
   "source": [
    "by src_dest_enc: gen lag_pr_rescue=pr_rescue[_n-1] \n",
    "by src_dest_enc: gen lag_migrant_stock=migrant_stock[_n-12] \n",
    "\n",
    "gen log_lag_migrant_stock = log(lag_migrant_stock)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T04:59:47.452549Z",
     "iopub.status.busy": "2024-02-01T04:59:47.451516Z",
     "iopub.status.idle": "2024-02-01T04:59:48.212821Z",
     "shell.execute_reply": "2024-02-01T04:59:48.210725Z",
     "shell.execute_reply.started": "2024-02-01T04:59:47.451516Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "label var lag_pr_rescue \"Probability of rescue (previous month)\"\n",
    "label var log_lag_migrant_stock \"Log migrant stock (previous year)\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Regressions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T04:59:48.217076Z",
     "iopub.status.busy": "2024-02-01T04:59:48.216799Z",
     "iopub.status.idle": "2024-02-01T04:59:48.957786Z",
     "shell.execute_reply": "2024-02-01T04:59:48.955781Z",
     "shell.execute_reply.started": "2024-02-01T04:59:48.217076Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "local fragile_states_vars  ///\n",
    "    src_security_app src_fact_elites src_group_grievance ///\n",
    "    src_economy src_econ_inequality src_human_flight ///\n",
    "    src_state_legitimacy src_public_services  src_human_rights  ///\n",
    "    src_demogr_pressures src_refugees_and_idps src_ext_intervention \n",
    "\n",
    "local diaspora_vars lag_migrant_stock \n",
    "\n",
    "local log_fragile_states_vars  ///\n",
    "    log_src_security_app  log_src_fact_elites log_src_group_grievance ///\n",
    "    log_src_economy log_src_econ_inequality log_src_human_flight  ///\n",
    "    log_src_state_legitimacy log_src_public_services  log_src_human_rights  ///\n",
    "    log_src_demogr_pressures log_src_refugees_and_idps log_src_ext_intervention \n",
    "\n",
    "local log_diaspora_vars log_lag_migrant_stock "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Linear specs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T04:59:48.961129Z",
     "iopub.status.busy": "2024-02-01T04:59:48.960131Z",
     "iopub.status.idle": "2024-02-01T04:59:51.425331Z",
     "shell.execute_reply": "2024-02-01T04:59:51.425331Z",
     "shell.execute_reply.started": "2024-02-01T04:59:48.961129Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\n",
      "(est1 stored)\n",
      "\n",
      "\n",
      "(est2 stored)\n",
      "\n",
      "\n",
      "(est3 stored)\n",
      "\n",
      "\n",
      "(est4 stored)\n",
      "\n",
      "\n",
      "\n",
      "------------------------------------------------------------------------\n",
      "                           (1)          (2)          (3)          (4)   \n",
      "                     log_inf~n    log_inf~n    log_inf~n    log_inf~n   \n",
      "------------------------------------------------------------------------\n",
      "Probability of res~      7.589***     9.272***     7.482***     8.956***\n",
      "                       (0.950)      (1.711)      (0.937)      (1.088)   \n",
      "\n",
      "FSI: Security Appa~s     0.067        0.538***    -0.693       -0.942   \n",
      "                       (0.154)      (0.182)      (0.433)      (0.672)   \n",
      "\n",
      "FSI: Factionalized~s     0.018       -0.178        0.722        1.396   \n",
      "                       (0.182)      (0.168)      (0.698)      (1.365)   \n",
      "\n",
      "FSI: Group Grievance     0.206       -0.450*      -0.638       -0.731   \n",
      "                       (0.167)      (0.227)      (0.456)      (0.874)   \n",
      "\n",
      "FSI: Economic Decl~e    -0.170       -0.126       -0.199       -1.035   \n",
      "                       (0.167)      (0.260)      (0.486)      (0.784)   \n",
      "\n",
      "FSI: Uneven Econom~t    -0.424        0.272        0.060       -0.260   \n",
      "                       (0.268)      (0.445)      (0.477)      (0.603)   \n",
      "\n",
      "FSI: Human Flight ~i     0.491***     0.251        0.526        0.012   \n",
      "                       (0.172)      (0.288)      (0.434)      (0.901)   \n",
      "\n",
      "FSI: State Legitim~y     0.151        1.032***     0.662        0.503   \n",
      "                       (0.253)      (0.305)      (0.667)      (0.906)   \n",
      "\n",
      "FSI: Public Services     0.179       -0.548*       0.286        0.313   \n",
      "                       (0.276)      (0.281)      (0.427)      (0.955)   \n",
      "\n",
      "FSI: Human Rights ~a    -0.225       -1.244***    -0.618        0.717   \n",
      "                       (0.272)      (0.346)      (0.654)      (1.077)   \n",
      "\n",
      "FSI: Demographic P~s     0.276        0.438       -0.168       -0.540   \n",
      "                       (0.278)      (0.341)      (0.520)      (1.095)   \n",
      "\n",
      "FSI: Refugees and ~s    -0.119        0.232        0.317        0.011   \n",
      "                       (0.262)      (0.214)      (0.425)      (0.701)   \n",
      "\n",
      "FSI: External Inte~n    -0.034        0.069        1.438**      1.574   \n",
      "                       (0.190)      (0.199)      (0.699)      (1.304)   \n",
      "\n",
      "log_distance            -1.725***    -0.969*                            \n",
      "                       (0.412)      (0.524)                             \n",
      "\n",
      "Log migrant stock ~r                  0.588***                 -1.388   \n",
      "                                    (0.205)                   (1.123)   \n",
      "\n",
      "Month FE                   Yes          Yes          Yes          Yes   \n",
      "\n",
      "Destination FE             Yes          Yes                             \n",
      "\n",
      "Origin-Destination~E                                 Yes          Yes   \n",
      "------------------------------------------------------------------------\n",
      "Adjusted R2               0.44         0.41         0.67         0.57   \n",
      "N Obs.                   2,726        1,152        2,726        1,152   \n",
      "------------------------------------------------------------------------\n",
      "Standard errors in parentheses\n",
      "\n",
      "* p<0.10, ** p<0.05, *** p<0.01\n"
     ]
    }
   ],
   "source": [
    "est clear \n",
    "\n",
    "qui reghdfe log_inferred_n lag_pr_rescue `fragile_states_vars' log_distance, absorb(month dest_iso) vce(cluster src_dest) \n",
    "eststo\n",
    "\n",
    "\n",
    "qui reghdfe log_inferred_n lag_pr_rescue `fragile_states_vars' `log_diaspora_vars' log_distance  , absorb(month dest_iso) vce(cluster src_dest) \n",
    "eststo\n",
    "\n",
    "qui reghdfe log_inferred_n lag_pr_rescue `fragile_states_vars'  , absorb(month src_dest) vce(cluster src_dest) \n",
    "eststo\n",
    "\n",
    "qui  reghdfe log_inferred_n lag_pr_rescue `fragile_states_vars'  `log_diaspora_vars', absorb(month src_dest) vce(cluster src_dest) \n",
    "eststo\n",
    "\n",
    "estfe *, labels(month \"Month FE\" src_iso \"Origin FE\" dest_iso \"Destination FE\" src_dest \"Origin-Destination FE\")\n",
    "\n",
    "esttab , ///\n",
    "        replace  varwidth(20)   ///\n",
    "        se(3) b(3) label compress noconstant           ///\n",
    "        stats(r2_a  N          ,                       ///\n",
    "        fmt(   %9.2fc    %10.0fc     )                 ///\n",
    "        label(\"Adjusted R2\"  \"N Obs.\" ))  ///\n",
    "        star(* 0.10 ** 0.05 *** 0.01)     ///\n",
    "        addnotes(\"\") /// \n",
    "        indicate(`r(indicate_fe)', labels(\"Yes\" \"\")) \n",
    "//\n",
    "\n",
    "// /*drop(src_dest_FE* quarter_FE* src_iso_FE* dest_iso_FE*) nobaselevels*/ ///"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Preferred specs (PPML HDFE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T04:59:51.425331Z",
     "iopub.status.busy": "2024-02-01T04:59:51.425331Z",
     "iopub.status.idle": "2024-02-01T04:59:54.951505Z",
     "shell.execute_reply": "2024-02-01T04:59:54.951505Z",
     "shell.execute_reply.started": "2024-02-01T04:59:51.425331Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\n",
      "(est1 stored)\n",
      "\n",
      "\n",
      "(est2 stored)\n",
      "\n",
      "\n",
      "(est3 stored)\n",
      "\n",
      "\n",
      "(est4 stored)\n",
      "\n",
      "\n",
      "\n",
      "------------------------------------------------------------------------\n",
      "                           (1)          (2)          (3)          (4)   \n",
      "                     inferre~n    inferre~n    inferre~n    inferre~n   \n",
      "------------------------------------------------------------------------\n",
      "Probability of res~      6.271***     6.501***     6.206***     5.404***\n",
      "                       (0.774)      (1.159)      (0.690)      (0.848)   \n",
      "\n",
      "FSI: Security Appa~s     0.163        0.342**      0.231       -0.739*  \n",
      "                       (0.136)      (0.162)      (0.282)      (0.422)   \n",
      "\n",
      "FSI: Factionalized~s    -0.164       -0.283***    -0.275        1.114   \n",
      "                       (0.124)      (0.109)      (0.348)      (0.832)   \n",
      "\n",
      "FSI: Group Grievance     0.165       -0.128       -0.883***    -0.245   \n",
      "                       (0.119)      (0.236)      (0.299)      (0.382)   \n",
      "\n",
      "FSI: Economic Decl~e    -0.000       -0.055        0.211       -0.330   \n",
      "                       (0.112)      (0.166)      (0.313)      (0.395)   \n",
      "\n",
      "FSI: Uneven Econom~t    -0.235        0.281        0.008       -0.132   \n",
      "                       (0.187)      (0.353)      (0.370)      (0.383)   \n",
      "\n",
      "FSI: Human Flight ~i     0.454***     0.131        0.954**     -0.211   \n",
      "                       (0.157)      (0.221)      (0.396)      (0.483)   \n",
      "\n",
      "FSI: State Legitim~y     0.100        0.675***     1.305***     1.187*  \n",
      "                       (0.153)      (0.238)      (0.409)      (0.627)   \n",
      "\n",
      "FSI: Public Services     0.355**      0.047        0.003        0.776   \n",
      "                       (0.156)      (0.174)      (0.409)      (0.653)   \n",
      "\n",
      "FSI: Human Rights ~a    -0.025       -0.587**     -1.109***    -0.234   \n",
      "                       (0.138)      (0.281)      (0.371)      (0.720)   \n",
      "\n",
      "FSI: Demographic P~s     0.390*       0.249       -0.021       -0.789   \n",
      "                       (0.200)      (0.232)      (0.456)      (0.643)   \n",
      "\n",
      "FSI: Refugees and ~s    -0.337*      -0.281*       0.031       -0.330   \n",
      "                       (0.176)      (0.162)      (0.295)      (0.429)   \n",
      "\n",
      "FSI: External Inte~n    -0.312*       0.010        0.658        0.816   \n",
      "                       (0.163)      (0.176)      (0.528)      (0.554)   \n",
      "\n",
      "log_distance            -1.305***    -0.692*                            \n",
      "                       (0.237)      (0.413)                             \n",
      "\n",
      "Log migrant stock ~r                  0.414*                   -2.974***\n",
      "                                    (0.212)                   (0.978)   \n",
      "\n",
      "Month FE                   Yes          Yes          Yes          Yes   \n",
      "\n",
      "Destination FE             Yes          Yes                             \n",
      "\n",
      "Origin-Destination~E                                 Yes          Yes   \n",
      "------------------------------------------------------------------------\n",
      "Pseudo R2                 0.57         0.49         0.74         0.69   \n",
      "N Obs.                   2,726        1,152        2,679        1,152   \n",
      "------------------------------------------------------------------------\n",
      "Standard errors in parentheses\n",
      "\n",
      "* p<0.10, ** p<0.05, *** p<0.01\n"
     ]
    }
   ],
   "source": [
    "est clear \n",
    "\n",
    "qui ppmlhdfe inferred_n lag_pr_rescue `fragile_states_vars' log_distance, absorb(month dest_iso) vce(cluster src_dest) d\n",
    "eststo\n",
    "\n",
    "\n",
    "qui ppmlhdfe inferred_n lag_pr_rescue `fragile_states_vars' `log_diaspora_vars' log_distance  , absorb(month dest_iso) vce(cluster src_dest) d\n",
    "eststo\n",
    "\n",
    "qui ppmlhdfe inferred_n lag_pr_rescue `fragile_states_vars'  , absorb(month src_dest) vce(cluster src_dest) d\n",
    "eststo\n",
    "\n",
    "qui  ppmlhdfe inferred_n lag_pr_rescue `fragile_states_vars'  `log_diaspora_vars', absorb(month src_dest) vce(cluster src_dest) d\n",
    "eststo\n",
    "\n",
    "estfe *, labels(month \"Month FE\" src_iso \"Origin FE\" dest_iso \"Destination FE\" src_dest \"Origin-Destination FE\")\n",
    "\n",
    "\n",
    "esttab , ///\n",
    "        replace  varwidth(20)   ///\n",
    "        se(3) b(3) label compress noconstant           ///\n",
    "        stats(r2_p  N          ,                       ///\n",
    "        fmt(   %9.2fc    %10.0fc     )                 ///\n",
    "        label(\"Pseudo R2\"  \"N Obs.\" ))  ///\n",
    "        star(* 0.10 ** 0.05 *** 0.01)     ///\n",
    "        addnotes(\"\") /// \n",
    "        indicate(`r(indicate_fe)', labels(\"Yes\" \"\")) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-04T20:49:28.765844Z",
     "iopub.status.busy": "2024-01-04T20:49:28.765844Z",
     "iopub.status.idle": "2024-01-04T20:49:30.158955Z",
     "shell.execute_reply": "2024-01-04T20:49:30.158955Z",
     "shell.execute_reply.started": "2024-01-04T20:49:28.765844Z"
    }
   },
   "source": [
    "#### Reset on preferred spec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T04:59:54.956121Z",
     "iopub.status.busy": "2024-02-01T04:59:54.956121Z",
     "iopub.status.idle": "2024-02-01T04:59:59.845277Z",
     "shell.execute_reply": "2024-02-01T04:59:59.845277Z",
     "shell.execute_reply.started": "2024-02-01T04:59:54.956121Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\n",
      "\n",
      "(58 missing values generated)\n",
      "\n",
      "(58 missing values generated)\n",
      "\n",
      "\n",
      "\n",
      " ( 1)  fit2 = 0\n",
      "\n",
      "           chi2(  1) =   30.72\n",
      "         Prob > chi2 =    0.0000\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "(1,632 missing values generated)\n",
      "\n",
      "(1,632 missing values generated)\n",
      "\n",
      "\n",
      "\n",
      " ( 1)  fit2 = 0\n",
      "\n",
      "           chi2(  1) =    0.36\n",
      "         Prob > chi2 =    0.5502\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "(58 missing values generated)\n",
      "\n",
      "(105 missing values generated)\n",
      "\n",
      "\n",
      "\n",
      " ( 1)  fit2 = 0\n",
      "\n",
      "           chi2(  1) =   81.52\n",
      "         Prob > chi2 =    0.0000\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "(1,632 missing values generated)\n",
      "\n",
      "(1,632 missing values generated)\n",
      "\n",
      "\n",
      "\n",
      " ( 1)  fit2 = 0\n",
      "\n",
      "           chi2(  1) =    3.17\n",
      "         Prob > chi2 =    0.0752\n"
     ]
    }
   ],
   "source": [
    "//  Reset test on preferred specification\n",
    "\n",
    "qui ppmlhdfe inferred_n lag_pr_rescue `fragile_states_vars' log_distance , absorb(month dest_iso) vce(cluster src_dest) d\n",
    "\n",
    "cap drop fit \n",
    "cap drop fit2\n",
    "predict fit, xb\n",
    "gen fit2=(fit +_ppmlhdfe_d)^2\n",
    "\n",
    "qui ppmlhdfe inferred_n lag_pr_rescue `fragile_states_vars' log_distance fit2 , absorb(month dest_iso) vce(cluster src_dest) d\n",
    "\n",
    "test fit2=0\n",
    "\n",
    "////// PASSING\n",
    "\n",
    "qui ppmlhdfe inferred_n lag_pr_rescue `fragile_states_vars' log_distance  `log_diaspora_vars' , absorb(month dest_iso) vce(cluster src_dest) d\n",
    "\n",
    "cap drop fit \n",
    "cap drop fit2\n",
    "predict fit, xb\n",
    "gen fit2=(fit +_ppmlhdfe_d)^2\n",
    "\n",
    "qui ppmlhdfe inferred_n lag_pr_rescue `fragile_states_vars' log_distance `log_diaspora_vars' fit2 , absorb(month dest_iso) vce(cluster src_dest) d\n",
    "\n",
    "test fit2=0\n",
    "\n",
    "//////////\n",
    "\n",
    "\n",
    "qui ppmlhdfe inferred_n lag_pr_rescue `fragile_states_vars' , absorb(month src_dest) vce(cluster src_dest) d\n",
    "\n",
    "cap drop fit \n",
    "cap drop fit2\n",
    "predict fit, xb\n",
    "gen fit2=(fit +_ppmlhdfe_d)^2\n",
    "\n",
    "qui ppmlhdfe inferred_n lag_pr_rescue `fragile_states_vars' fit2 , absorb(month src_dest) vce(cluster src_dest) d\n",
    "\n",
    "test fit2=0\n",
    "\n",
    "////// PASSING (5%)\n",
    "\n",
    "qui ppmlhdfe inferred_n lag_pr_rescue `fragile_states_vars' `log_diaspora_vars' , absorb(month src_dest) vce(cluster src_dest) d\n",
    "\n",
    "cap drop fit \n",
    "cap drop fit2\n",
    "predict fit, xb\n",
    "gen fit2=(fit +_ppmlhdfe_d)^2\n",
    "\n",
    "qui ppmlhdfe inferred_n lag_pr_rescue `fragile_states_vars' `log_diaspora_vars' fit2 , absorb(month src_dest) vce(cluster src_dest) d\n",
    "\n",
    "test fit2=0\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Interpreting results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-04T19:19:40.126083Z",
     "iopub.status.busy": "2024-01-04T19:19:40.126083Z",
     "iopub.status.idle": "2024-01-04T19:19:40.886260Z",
     "shell.execute_reply": "2024-01-04T19:19:40.882141Z",
     "shell.execute_reply.started": "2024-01-04T19:19:40.126083Z"
    }
   },
   "source": [
    "Let's consider a 1 percentage point increase in x when x is a proportion. Then \n",
    "\n",
    "$$ y = \\exp(X\\beta) $$\n",
    "\n",
    "and the change in y can be expressed as:\n",
    "\n",
    "$$ \\Delta y \\quad=\\quad \\frac{\\exp((X+0.01)\\beta) \\quad-\\quad \\exp(X\\beta)}{\\exp(X\\beta)} \\quad=\\quad \\exp(0.01 \\beta) - 1$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T04:59:59.845277Z",
     "iopub.status.busy": "2024-02-01T04:59:59.845277Z",
     "iopub.status.idle": "2024-02-01T05:00:01.231769Z",
     "shell.execute_reply": "2024-02-01T05:00:01.228488Z",
     "shell.execute_reply.started": "2024-02-01T04:59:59.845277Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "6.5005367\n",
      "\n",
      ".06716475\n"
     ]
    }
   ],
   "source": [
    "qui ppmlhdfe inferred_n lag_pr_rescue `fragile_states_vars' `log_diaspora_vars' log_distance, absorb(month dest_iso) vce(cluster src_dest) d\n",
    "di _b[lag_pr_rescue]\n",
    "di (exp(_b[lag_pr_rescue]*.01) - 1)\n",
    "// So a one percentage in pr_rescue (proportion) leads to a 6.7% increase in y"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Stata",
   "language": "stata",
   "name": "stata"
  },
  "language_info": {
   "codemirror_mode": "stata",
   "file_extension": ".do",
   "mimetype": "text/x-stata",
   "name": "stata",
   "version": "15.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
